package com.xvaca.spcms.db;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.apache.log4j.Logger;

/**
 * 数据库连接池
 * 
 * @author zhanglei
 * 
 */
public class DBConnector {

	private static Logger logger = Logger.getLogger(DBConnector.class);

	public static BasicDataSource ds = null;

	static {
		initDataSource();

	}

	/**
	 * 获取数据库连接
	 * 
	 * @return
	 */
	public static Connection getConnection() {
		
		if (ds == null) {
			initDataSource();
		}
		
		logger.debug("活动连接数：" + ds.getNumActive());
		logger.debug("空闲连接数：" + ds.getNumIdle());

		long t1 = System.currentTimeMillis();
		Connection con = null;

		if (ds != null) {
			try {
				con = ds.getConnection();
			} catch (Exception e) {
				logger.error("获取数据库连接出错", e);
			}

			// try {
			// con.setAutoCommit(false);
			// } catch (SQLException e) {
			// e.printStackTrace();
			// }
			long t2 = System.currentTimeMillis();
			logger.debug("获取Connection耗时" + (t2 - t1) + "ms");
			return con;
		}
		return con;
	}

	/**
	 * 执行清理过程
	 * 
	 * <li>关闭数据库连接</li> <li>关闭语句对象</li> <li>关闭结果集</li>
	 * 
	 * @param conn
	 * @param stmt
	 * @param rs
	 */
	public static void closeAll(Connection conn, Statement stmt, ResultSet rs) {
		try {
			if (rs != null) {
				rs.close();
				rs = null;
			}

			if (stmt != null) {
				stmt.close();
				stmt = null;
			}

			if (conn != null) {
				conn.close();
				conn = null;
			}
		} catch (SQLException sqle) {
			logger.error("关闭数据库连接异常", sqle);
		}
	}
	
	/**
	 * 执行清理过程
	 * 
	 * <li>关闭数据库连接</li>
	 * <li>关闭语句对象</li>
	 * <li>关闭结果集</li>
	 * 
	 * @param conn
	 * @param pstmt
	 * @param rs
	 */
	public static void closeAll(Connection conn, PreparedStatement pstmt, ResultSet rs) {
		try {
			if (rs != null) {
				rs.close();
				rs = null;
			}

			if (pstmt != null) {
				pstmt.close();
				pstmt = null;
			}

			if (conn != null) {
				conn.close();
				conn = null;
			}
		} catch (SQLException sqle) {
			logger.error("关闭数据库连接异常", sqle);
		}
	}

	public static void main(String args[]) throws Exception {

		Connection conn = DBConnector.getConnection();
		//Connection connq = DBConnector.getConnection();

		// java.sql.PreparedStatement pre =
		// conn.prepareStatement(" select * from hs_book_userinfo");
		// pre.executeQuery();
		// conn.close();
		// ds.close();
		//com.mysql.jdbc.Driver d = null;
		System.out.println(conn);
	}
	
	public static void initDataSource() {
		try {
			Properties prop = new Properties();
			InputStream in = DBConnector.class
					.getResourceAsStream("../../../../jdbc.properties");
			prop.load(in);
			in.close();
			ds = (BasicDataSource) BasicDataSourceFactory
					.createDataSource(prop);
			logger.debug("创建DataSource成功");
			logger.debug("初始化连接数：" + ds.getInitialSize());
		} catch (Exception e) {
			logger.error("创建DataSource异常。", e);
		}
	}

}